
"use strict";
angular.module('app.project').controller('BudgetCtrl', function ($scope,$http,$rootScope, $stateParams,$cookieStore) {
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        plan: 0,
        projectid: '',
        onChange: function () {
        }
    };
    $scope.addobj = {};
    $scope.userid = $cookieStore.get('userid');

    $scope.fid = $cookieStore.get('userinfo').fid;
    // 获得项目数据
    $http.post(baseurl+'/list/select/project',$scope.page_obj).then(function(res){
        if (res.data.ret == 1) {
            if(res.data.rows.length > 0){

                $scope.projects = res.data.rows;
                for(var i = 0; i < res.data.rows.length; i++){
                    $scope.projects[i].id = JSON.stringify($scope.projects[i].id)
                }

                $scope.addobj.projectid = $scope.projects[0].id;
            }

        }
    });
    $scope.sends = function(){
        $scope.page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage
        };
        var query = '?projectid=='+$scope.paginationConf.projectid;
        // 获得预算成本数据
        $http.post(baseurl+'/list/select/view_budget_project'+query,$scope.page_obj).then(function(res){
            if (res.data.ret == 1 && res.data.rows != null) {
                var arr = res.data.rows; 
                for (var i = 0; i < arr.length; i++) {
                    var obj = arr[i];
                    var time = obj.time;
                    obj.time = moment(time).format("YYYY-MM-DD HH:mm:ss");
                    if (obj.time == 'Invalid date') {
                            obj.time = time;
                    }
                }
                $scope.budget = arr;
                $scope.paginationConf.totalItems = res.data.records;
            }
        })
    };


    // 新添加预算成本数据
    $scope.sure_add = function () {
        if(!$scope.addobj.name ||  $scope.addobj.name === ''){
            new Notify('info','载体名称不能为空');
            return;
        }
        if(!$scope.addobj.design_num ||  $scope.addobj.design_num === ''){
            new Notify('info','设计值不能为空');
            return;
        }
        if(!$scope.addobj.equ_num ||  $scope.addobj.equ_num === ''){
            new Notify('info','换算值不能为空');
            return;
        }
        if(!$scope.addobj.actural_num ||  $scope.addobj.actural_num === ''){
            new Notify('info','实际用量不能为空');
            return;
        }
        $http.post(baseurl+'/data/budget',$scope.addobj).then(function(res){
            if (res.data.ret == 1) {
                $("#addAlert").modal('hide');
                $scope.addobj = null;
                new Notify('success', '添加成功');
                $scope.sends();
            }
        })
    };

    // 预算成本编辑框回绑数据
    $scope.Edit_pro = function(obj){
        $scope.editid = obj.id;
        $scope.editprojectid = obj.proname;
        $scope.editname = obj.name;
        $scope.editobj = {
            design_num:obj.design_num,
            equ_num:obj.equ_num,
            actural_num:obj.actural_num
        };
    };

    // 预算成本编辑框确定
    $scope.sure_edit = function () {
        $http.put(baseurl+'/data/budget?id=='+$scope.editid,$scope.editobj).then(function(res){
            if (res.data.ret == 1) {
                $("#editAlert").modal('hide');           
                new Notify('success','修改成功');
                $scope.sends();
            }
        })
    };

    $scope.showDelDialog = function(id){
        $scope.dele_id = id
    };

    // 删除这条预算成本数据
    $scope.sure_del = function () {
        $http.delete(baseurl+'/data/budget?id=='+$scope.dele_id).then(function(res){
            if (res.data.ret == 1) {
                $("#delAlert").modal('hide');
                $scope.sends();
                new Notify('success','删除成功');
            }
        })
    };

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.sends);
});